package org.luxor.commons.core.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;

/**
 * Base64编解码工具
 *
 * @author admin @date 2019/9/17 22:42
 */
public class Base64Utils {

    /**
     * [编码] - 将字符串转换为Base64
     */
    public static String encode(String text) {
        try {
            return StringUtils.toString(Base64.encodeBase64String(text.getBytes(StrKit.CHARSET_NAME)));
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    /**
     * [解码] - 将Base64转换为字符串
     */
    public static String decode(String base64) {
        try {
            return StringUtils.toString(Base64.decodeBase64(base64.getBytes(StrKit.CHARSET_NAME)));
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }


//    public static void main(String[] args) {
//        String urlStr = Base64Utils.encode("http://www.baidu.com?&id=123");
//        System.out.println(urlStr);
//        System.out.println(Base64Utils.decode(urlStr));
//    }

}
